package com.zhangzhan.gulimallproduct.web;

import com.zhangzhan.gulimallproduct.entity.CategoryEntity;
import com.zhangzhan.gulimallproduct.service.CategoryService;
import com.zhangzhan.gulimallproduct.vos.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/6/21 2:17
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redisson;


    @RequestMapping(value = {"/", "/index.html"}, method = RequestMethod.GET)
    public String index(Model model) {

        List<CategoryEntity> categoryEntities = categoryService.listLevel1();

        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    @ResponseBody
    @RequestMapping(value = {"/index/category.json"}, method = RequestMethod.GET)
    public Map<String, List<Catelog2Vo>> indexCategory() {

        return categoryService.indexCategory();
    }

    @ResponseBody
    @RequestMapping(value = {"/hello"}, method = RequestMethod.GET)
    public String hello() {
        RLock lock = redisson.getLock("my-lock");

        lock.lock();

        try {
            System.out.println("获取锁...." + Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            System.out.println("解锁...." + Thread.currentThread().getId());
            lock.unlock();
        }


        return "hello";
    }

}
